/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import rmiclient.Service;
import util.Employee;

/**
 * 
 * @author salim
 */
public class AUemployeeDialog extends javax.swing.JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * A return status code - returned if Cancel button has been pressed
	 */
	public static final int RET_CANCEL = 0;
	/**
	 * A return status code - returned if OK button has been pressed
	 */
	public static final int RET_OK = 1;

	private DialogType dialogType = DialogType.ADD_EMPLOYEE_DIALOG;

	public void setCurrentComboBoxItem(String cont) {
		institutionsComboBox.setSelectedItem(cont);
	}

	/**
	 * Creates new form NewOkCancelDialog
	 * 
	 * @param parent
	 * @param modal
	 */
	public AUemployeeDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		// Close the dialog when Esc is pressed
		String cancelName = "cancel";
		InputMap inputMap = getRootPane().getInputMap(
				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
		ActionMap actionMap = getRootPane().getActionMap();
		actionMap.put(cancelName, new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				doClose(RET_CANCEL);
			}
		});
	}

	/**
	 * @return the return status of this dialog - one of RET_OK or RET_CANCEL
	 */
	public int getReturnStatus() {
		return returnStatus;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel2 = new javax.swing.JPanel();
		jLabel5 = new javax.swing.JLabel();
		institutionsComboBox = new javax.swing.JComboBox();
		jLabel1 = new javax.swing.JLabel();
		firstNameTextField = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		lastNameTextField = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		cinTextField = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		telTextField = new javax.swing.JTextField();
		jPanel1 = new javax.swing.JPanel();
		okButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();

		addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent evt) {
				closeDialog(evt);
			}
		});

		jLabel5.setText("Institution");
		jPanel2.add(jLabel5);

		institutionsComboBox
				.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
						"FST", "ENIT", "ISI", "INSAT", "ENSIT", "ENSI" }));
		institutionsComboBox.setPreferredSize(new java.awt.Dimension(100, 30));
		institutionsComboBox
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						institutionsComboBoxActionPerformed(evt);
					}
				});
		jPanel2.add(institutionsComboBox);

		jLabel1.setText("first name");
		jPanel2.add(jLabel1);

		firstNameTextField.setPreferredSize(new java.awt.Dimension(100, 30));
		firstNameTextField
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						firstNameTextFieldActionPerformed(evt);
					}
				});
		jPanel2.add(firstNameTextField);

		jLabel2.setText("last name");
		jPanel2.add(jLabel2);

		lastNameTextField.setPreferredSize(new java.awt.Dimension(100, 30));
		lastNameTextField
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						lastNameTextFieldActionPerformed(evt);
					}
				});
		jPanel2.add(lastNameTextField);

		jLabel3.setText("cin");
		jPanel2.add(jLabel3);

		cinTextField.setPreferredSize(new java.awt.Dimension(100, 30));
		cinTextField.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cinTextFieldActionPerformed(evt);
			}
		});
		jPanel2.add(cinTextField);

		jLabel4.setText("tel");
		jPanel2.add(jLabel4);

		telTextField.setPreferredSize(new java.awt.Dimension(100, 30));
		telTextField.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				telTextFieldActionPerformed(evt);
			}
		});
		jPanel2.add(telTextField);

		getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);

		okButton.setText("OK");
		okButton.setPreferredSize(new java.awt.Dimension(100, 30));
		okButton.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				okButtonMouseClicked(evt);
			}
		});
		okButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okButtonActionPerformed(evt);
			}
		});
		okButton.addAncestorListener(new javax.swing.event.AncestorListener() {
			@Override
			public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
			}

			@Override
			public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
				okButtonAncestorAdded(evt);
			}

			@Override
			public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
			}
		});
		jPanel1.add(okButton);
		getRootPane().setDefaultButton(okButton);

		cancelButton.setText("Cancel");
		cancelButton.setPreferredSize(new java.awt.Dimension(100, 30));
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});
		jPanel1.add(cancelButton);

		getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

		pack();
	}// </editor-fold>

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
		doClose(RET_CANCEL);
	}

	/**
	 * Closes the dialog
	 */
	private void closeDialog(java.awt.event.WindowEvent evt) {
		doClose(RET_CANCEL);
	}

	private void firstNameTextFieldActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void lastNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void cinTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void telTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void okButtonMouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
	}

	private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {

		try {
			if (dialogType == DialogType.ADD_EMPLOYEE_DIALOG) {

				int ret = Service.getServiceInstance().addEmployee(
						new Employee(firstNameTextField.getText(),
								lastNameTextField.getText(), cinTextField
										.getText(), telTextField.getText(),
								institutionsComboBox.getSelectedItem()
										.toString()));

				if (ret == -1) {
					JOptionPane.showMessageDialog(this, "Server failure.",
							"Data entry", JOptionPane.INFORMATION_MESSAGE);
				} else if (ret == 0) {
					JOptionPane.showMessageDialog(this, "Unknown failure.",
							"Data entry", JOptionPane.INFORMATION_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(
							this,
							"First name: "
									+ firstNameTextField.getText()
									+ "\nLast name: "
									+ lastNameTextField.getText()
									+ "\nNationel Id Number: "
									+ cinTextField.getText()
									+ "\nPhone number: "
									+ telTextField.getText()
									+

									" successfully added to "
									+ institutionsComboBox.getSelectedItem()
											.toString(), "Data entry",
							JOptionPane.INFORMATION_MESSAGE);
				}

			} else {

				int ret = Service.getServiceInstance().updateEmployee(
						new Employee(firstNameTextField.getText(),
								lastNameTextField.getText(), cinTextField
										.getText(), telTextField.getText(),
								institutionsComboBox.getSelectedItem()
										.toString()));

				if (ret == -1) {
					JOptionPane.showMessageDialog(this, "Server failure.",
							"Data entry", JOptionPane.INFORMATION_MESSAGE);
				} else if (ret == 0) {
					JOptionPane.showMessageDialog(this, "Unknown failure.",
							"Data entry", JOptionPane.INFORMATION_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(
							this,

							"First name: "
									+ firstNameTextField.getText()
									+ "\nLast name: "
									+ lastNameTextField.getText()
									+ "\nNationel Id Number: "
									+ cinTextField.getText()
									+ "\nPhone number: "
									+ telTextField.getText()
									+

									" successfully updated in "
									+ institutionsComboBox.getSelectedItem()
											.toString(), "Data entry",
							JOptionPane.INFORMATION_MESSAGE);
				}

			}
		} catch (Exception e) {
			JOptionPane
					.showMessageDialog(
							this,
							"Please check service configuration or the service backbone.",
							"Unable to invoke service",
							JOptionPane.WARNING_MESSAGE);
		}

		doClose(RET_OK);
	}

	private void okButtonAncestorAdded(javax.swing.event.AncestorEvent evt) {
		// TODO add your handling code here:
	}

	private void institutionsComboBoxActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void doClose(int retStatus) {
		returnStatus = retStatus;
		setVisible(false);
		dispose();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException
				| javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger
					.getLogger(AUemployeeDialog.class.getName()).log(
							java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the dialog */
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				AUemployeeDialog dialog = new AUemployeeDialog(
						new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	/**
	 * @return the dialogType
	 */
	public DialogType getDialogType() {
		return dialogType;
	}

	/**
	 * @param dialogType
	 *            the dialogType to set
	 */
	public void setDialogType(DialogType dialogType) {
		this.dialogType = dialogType;
	}

	// Variables declaration - do not modify
	private javax.swing.JButton cancelButton;
	private javax.swing.JTextField cinTextField;
	private javax.swing.JTextField firstNameTextField;
	@SuppressWarnings("rawtypes")
	private javax.swing.JComboBox institutionsComboBox;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JTextField lastNameTextField;
	private javax.swing.JButton okButton;
	private javax.swing.JTextField telTextField;
	// End of variables declaration

	private int returnStatus = RET_CANCEL;
}
